<?php
/**
 * Author: DefinitlyEvil
 * Created at: 2020/4/13 23:03
 */

namespace App\PublicAPI;


use App\Controller;
use App\Tools\ECSTools;
use App\Tools\Encryption;

class MainController extends Controller
{

    public $needAuth = false;

    public function version() {
        return [json_decode(file_get_contents('../client.json'), true), true];
    }

    public function test() {
        $token = Encryption::encrypt_encoded('test');
        $this->getRedis()->setex("key", 10, $token);

        $ret = [];
        $ret['enc'] = $token;
        $ret['dec'] = Encryption::decrypt_encoded($token);
        return [$ret, true];

        $result = ECSTools::createEcs();
        return [['result' => $result], true];
    }

    public function asset($params = []) {
        if(!isset($params['path']) or !is_string($params['path']) or substr($params['path'], 0, 1) !== '/') return ['invalid path', false];
        date_default_timezone_set('Asia/Shanghai');
        $t = date('YmdHi', time()-1620);
        $key = getenv('ASSET_CDN_KEY');
        $path = &$params['path'];
        $k = md5($key . $t . $path);
        return [
            ['asset' => [
                't' => $t,
                'k' => $k
            ]],
            true
        ];
    }
}